package com.nari.ruist.controller;

import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.nari.common.log.annotation.Log;
import com.nari.common.log.enums.BusinessType;
import com.nari.common.security.annotation.PreAuthorize;
import com.nari.ruist.domain.RstLogreport;
import com.nari.ruist.service.IRstLogreportService;
import com.nari.common.core.web.controller.BaseController;
import com.nari.common.core.web.domain.AjaxResult;
import com.nari.common.core.utils.poi.ExcelUtil;
import com.nari.common.core.web.page.TableDataInfo;
import org.springframework.web.client.RestTemplate;

/**
 * 日志统计 Controller
 *
 * @author nari
 * @date 2021-04-02
 */
@RestController
@RequestMapping("/logreport")
public class RstLogreportController extends BaseController {
    @Autowired
    private IRstLogreportService rstLogreportService;
    @Autowired
    private RestTemplate restTemplate;


    @Value("${nacosUrl}")
    private String nacosUrl;

    @PreAuthorize(hasPermi = "system:logreport:list")
    @GetMapping("/list")
    public TableDataInfo list(String pageNum,String pageSize) {
        String targetUrl = nacosUrl +
                "hasIpCount=true" +
                "&withInstances=false" +
                "&pageNo=" +pageNum+
                "&pageSize=" +pageSize+
                "&keyword=" +
                "&namespaceId=";
        TableDataInfo tableDataInfo=new TableDataInfo();
        try {
            ResponseEntity<String> responseEntity = this.restTemplate.getForEntity(targetUrl, String.class);
            JSONObject jsonBody = JSON.parseObject(responseEntity.getBody());
            List<Object> array= Arrays.asList(jsonBody.get("serviceList")) ;
            Integer count= (Integer) jsonBody.get("count");
            tableDataInfo.setRows(array);
            tableDataInfo.setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tableDataInfo;
    }

    /**
     * 导出日志统计 列表
     */
    @PreAuthorize(hasPermi = "system:logreport:export")
    @Log(title = "日志统计 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RstLogreport rstLogreport) throws IOException {
        List<RstLogreport> list = rstLogreportService.selectRstLogreportList(rstLogreport);
        ExcelUtil<RstLogreport> util = new ExcelUtil<RstLogreport>(RstLogreport.class);
        util.exportExcel(response, list, "logreport");
    }

    /**
     * 获取日志统计 详细信息
     */
    @PreAuthorize(hasPermi = "system:logreport:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(rstLogreportService.selectRstLogreportById(id));
    }

    /**
     * 新增日志统计
     */
    @PreAuthorize(hasPermi = "system:logreport:add")
    @Log(title = "日志统计 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RstLogreport rstLogreport) {
        return toAjax(rstLogreportService.insertRstLogreport(rstLogreport));
    }

    /**
     * 修改日志统计
     */
    @PreAuthorize(hasPermi = "system:logreport:edit")
    @Log(title = "日志统计 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RstLogreport rstLogreport) {
        return toAjax(rstLogreportService.updateRstLogreport(rstLogreport));
    }

    /**
     * 删除日志统计
     */
    @PreAuthorize(hasPermi = "system:logreport:remove")
    @Log(title = "日志统计 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(rstLogreportService.deleteRstLogreportByIds(ids));
    }
}
